<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <el-form-item prop="username" label="工号">
        <el-input v-model="dataForm.username" placeholder="工号"></el-input>
      </el-form-item>
      <el-form-item v-if="!dataForm.id" prop="password" label="密码" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item v-if="!dataForm.id" prop="confirmPassword" label="确认密码" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.confirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item prop="realName" label="真实姓名">
        <el-input v-model="dataForm.realName" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item prop="gender" label="性别">
        <ren-radio-group v-model="dataForm.gender" dict-type="gender"></ren-radio-group>
      </el-form-item>
      <!-- <el-form-item prop="email" label="邮箱">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item> -->
      <el-form-item prop="mobile" label="手机号">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <!-- <el-form-item prop="roleIdList" label="角色配置" class="role-list">
        <el-select v-model="dataForm.roleIdList" multiple placeholder="角色配置" disabled>
          <el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id"></el-option>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item prop="status" label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">停用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
  import { reactive, ref } from "vue";
  import baseService from "@/service/baseService";
  import { isEmail, isMobile } from "@/utils/utils";
  import { IObject } from "@/types/interface";
  import { ElMessage } from "element-plus";
  const emit = defineEmits(["refreshDataList"]);

  const visible = ref(false);
  const roleList = ref < any[] > ([]);
  const dataFormRef = ref();

  const dataForm = reactive({
    id: "",
    username: "",
    password: "",
    confirmPassword: "",
    realName: "",
    gender: 0,
    mobile: "",
    roleIdList: ['1789120315865456642'] as IObject[],
    status: 1
  });

  const validatePassword = (rule: any, value: string, callback: (e?: Error) => any): any => {
    if (!dataForm.id && !/\S/.test(value)) {
      return callback(new Error("必填项不能为空"));
    }
    callback();
  };
  const validateConfirmPassword = (rule: any, value: string, callback: (e?: Error) => any): any => {
    if (!dataForm.id && !/\S/.test(value)) {
      return callback(new Error("必填项不能为空"));
    }
    if (dataForm.password !== value) {
      return callback(new Error("确认密码与密码输入不一致"));
    }
    callback();
  };
  const validateMobile = (rule: any, value: string, callback: (e?: Error) => any): any => {
    if (value && !isMobile(value)) {
      return callback(new Error("手机格式错误"));
    }
    callback();
  };
  const rules = ref({
    username: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
    password: [{ validator: validatePassword, trigger: "blur" }],
    confirmPassword: [{ validator: validateConfirmPassword, trigger: "blur" }],
    realName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
    mobile: [{ validator: validateMobile, trigger: "blur" }]
  });

  const init = (value?: object) => {
    visible.value = true;
    if (value && value.id) {
      dataForm.majorId = value.majorId;
      dataForm.id = value.id;
      dataForm.username = value.userName;
      dataForm.realName = value.realName;
      dataForm.gender = value.gender;
      dataForm.mobile = value.mobile;
      dataForm.roleIdList = ['1789120315865456642'];
      dataForm.status = value.status;
    } else {
      if (dataFormRef.value) {
        dataFormRef.value.resetFields();
      }
      dataForm.id = '';
      dataForm.username = '';
      dataForm.password = '';
      dataForm.confirmPassword = '';
      dataForm.realName = '';
      dataForm.gender = '';
      dataForm.mobile = '';
      dataForm.roleIdList = ['1789120315865456642'];
      dataForm.status = '1';
    }
  };

  // 表单提交
  const dataFormSubmitHandle = () => {
    dataFormRef.value.validate((valid: boolean) => {
      if (!valid) {
        return false;
      }
      (!dataForm.id ? baseService.post : baseService.put)("/sys/user", {
        ...dataForm,
        roleIdList: [...dataForm.roleIdList,]
      }).then((res) => {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            visible.value = false;
            emit("refreshDataList");
          }
        });
      });
    });
  };

  defineExpose({
    init
  });
</script>

<style lang="less" scoped>
  .mod-sys__user {
    .role-list {
      .el-select {
        width: 100%;
      }
    }
  }
</style>